package com.bootdo.output.controller;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpSession;

import com.bootdo.stock.domain.StockDO;
import com.bootdo.stock.service.StockService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.ui.Model;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.bootdo.output.domain.OutStorageDetailDO;
import com.bootdo.output.domain.suppliersCatalog;
import com.bootdo.output.service.OutStorageDetailService;
import com.bootdo.output.utils.packageOutType;
import com.bootdo.provider.domain.GoodsInfoDO;
import com.bootdo.provider.service.GoodsInfoService;
import com.bootdo.system.domain.UserDO;
import com.bootdo.common.utils.PageUtils;
import com.bootdo.common.utils.Query;
import com.bootdo.common.utils.R;
import com.bootdo.common.utils.ShiroUtils;
import com.bootdo.goods.domain.TGoodsAllocationDO;
import com.bootdo.goods.service.TGoodsAllocationAloneService;
import com.bootdo.goods.service.TGoodsAllocationService;

/**
 * 出库订单明细表
 * 
 * @author chglee
 * @email 1992lcg@163.com
 * @date 2018-04-26 14:01:15
 */
 
@Controller
@RequestMapping("/output/outStorageDetail")
public class OutStorageDetailController {
	@Autowired
	private OutStorageDetailService outStorageDetailService;
	@Autowired
	private  packageOutType packageOutType;
	@Autowired
	private GoodsInfoService goodsInfoService;
	@Autowired
	private TGoodsAllocationService  tGoodsAllocationService;
	@Autowired
	private StockService stockService;
	@GetMapping()
//	@RequiresPermissions("output:outStorageDetail:outStorageDetail")
	String OutStorageDetail(@RequestParam(required = false) Long outstorageId,Model model,HttpSession session){
		session.removeAttribute("outId");
		session.setAttribute("outId", outstorageId);
	    return "output/outStorage/addOrder";
	}
	
	@ResponseBody
	@GetMapping("/list")
//	@RequiresPermissions("output:outStorageDetail:outStorageDetail")
	public List<OutStorageDetailDO> list(@RequestParam Map<String, Object> params,HttpSession session){
		Long s = (Long) session.getAttribute("outId");
		if(s==null){
			 return null;
		}
		params.put("outId",s )	;
		params.put("offset",0);
		params.put("limit",9999);

		//查询列表数据
        Query query = new Query(params);
		List<OutStorageDetailDO> outStorageDetailList = outStorageDetailService.list(query);
		for (OutStorageDetailDO outStorageDetailDO : outStorageDetailList) {
			outStorageDetailDO = packageOutType.packageOutStorageDetailPojo(outStorageDetailDO);
		}

		return outStorageDetailList;
	}



	@GetMapping("/add")
	String add(HttpSession session){
		UserDO userDO = ShiroUtils.getUser();
		Map<String,Object> Map = new HashMap<>();
		List<Long>  l =  new ArrayList<>();
		Map.put("outId",-userDO.getProviderId());
		List<OutStorageDetailDO> outStorageDetailDOS = outStorageDetailService.list(Map);
		for (OutStorageDetailDO o:  outStorageDetailDOS) {
			l.add(o.getDatilId());
		}
		Long [] ll = new Long[l.size()];
		l.toArray(ll);
		if(l.size()!=0){
			outStorageDetailService.batchRemove(ll);
		}

		session.removeAttribute("outId");
	    return "output/outStorage/addOrder";
	}

	@GetMapping("/edit/{datilId}")
//	@RequiresPermissions("output:outStorageDetail:edit")
	String edit(@PathVariable("datilId") Long datilId,Model model){
		OutStorageDetailDO outStorageDetail = outStorageDetailService.get(datilId);
		model.addAttribute("outStorageDetail", outStorageDetail);
	    return "output/outStorageDetail/edit";
	}
	
	/**
	 * 保存
	 */
	@ResponseBody
	@PostMapping("/save")
	public R save( OutStorageDetailDO outStorageDetail){
		if(outStorageDetailService.save(outStorageDetail)>0){
			return R.ok();
		}
		return R.error();
	}

	@ResponseBody
	@PostMapping("/saveDetail")
	public R saveDetail(String code,Long oudId,HttpSession session){
		UserDO userDO = ShiroUtils.getUser();
		Long s = (Long) session.getAttribute("outId");
		if(s==null){
			oudId = -userDO.getProviderId();
			session.setAttribute("outId", -userDO.getProviderId());
		}
		s = (Long) session.getAttribute("outId");
		Map<String,Object> Map = new HashMap<>();
		Map.put("providerId",userDO.getProviderId());
		Map.put("goodsCode",code);
		GoodsInfoDO goodsInfoDO = goodsInfoService.list(Map).get(0);
		if(s != -userDO.getProviderId()){
			stockService.update(goodsInfoDO.getGoodsId(),-1,-1,0,0);
		}
		OutStorageDetailDO outStorageDetail = new OutStorageDetailDO();
		outStorageDetail.setPrice("0");
		outStorageDetail.setDatilAmount(1);
		outStorageDetail.setDatilGoodsid(goodsInfoDO.getGoodsId());
		outStorageDetail.setDatilEamount(-1);
		outStorageDetail.setOutId(s);
		outStorageDetail.setTotalPrices("0.0");
		outStorageDetail.setDatilPricingManner(goodsInfoDO.getGoodsPackagingType());
		outStorageDetail.setDatilStandard(goodsInfoDO.getGoodsStandard());
		if(outStorageDetailService.save(outStorageDetail)>0){
			return R.ok();
		}
		return R.error();
	}
	/**
	 * 修改
	 */
	@ResponseBody
	@RequestMapping("/update")
	public R update( OutStorageDetailDO outStorageDetail,HttpSession session){
		UserDO userDO = ShiroUtils.getUser();
		StockDO stockDO = 	stockService.getStockByGsId(outStorageDetail.getDatilGoodsid());
		OutStorageDetailDO detailDO = outStorageDetailService.get(outStorageDetail.getDatilId());		if((outStorageDetail.getDatilAmount()-detailDO.getDatilAmount()) >	stockDO.getStockOutNum()){
			return R.error();
		}
		Long s = (Long) session.getAttribute("outId");
		if(s!=-userDO.getProviderId() && s!= null){

			Integer  oldNUm = detailDO.getDatilAmount();
			Integer newNum = outStorageDetail.getDatilAmount();
			if(oldNUm>newNum){//这是减少了商品数量
				stockService.update(outStorageDetail.getDatilGoodsid(),+(oldNUm-newNum),+(oldNUm-newNum),0,0);
			}else{//添加数量
				stockService.update(outStorageDetail.getDatilGoodsid(),-(newNum-oldNUm),-(newNum-oldNUm),0,0);
			}
		}
		Double integer = 	new Double(outStorageDetail.getPrice()) * new Double(outStorageDetail.getDatilAmount());
		outStorageDetail.setTotalPrices(integer.toString());
		outStorageDetailService.update(outStorageDetail);
		return R.ok();
	}
	
	/**
	 * 删除
	 */
	@PostMapping( "/remove")
	@ResponseBody
	public R remove( Long datilId){
		OutStorageDetailDO outStorageDetailDO = outStorageDetailService.get(datilId);
		stockService.update(outStorageDetailDO.getDatilGoodsid(),outStorageDetailDO.getDatilAmount(),outStorageDetailDO.getDatilAmount(),0,0);
		if(outStorageDetailService.remove(datilId)>0){
		return R.ok();
		}
		return R.error();
	}
	
	/**
	 * 删除
	 */
	@PostMapping( "/batchRemove")
	@ResponseBody
	public R remove(@RequestParam("ids[]") Long[] datilIds){
		outStorageDetailService.batchRemove(datilIds);
		return R.ok();
	}
	
	@ResponseBody
	@GetMapping("/goodlistlist")
	public PageUtils Goodlist(@RequestParam Map<String, Object> params){
		UserDO userDO = ShiroUtils.getUser();
		params.put("pid",userDO.getProviderId());
		Query query = new Query(params);
		List<StockDO> stockDOS = 	stockService.listP(query);
		int total = stockService.countP(query);
		PageUtils pageUtils = new PageUtils(stockDOS, total);
		return  pageUtils;
	}
	/* wslt 订单管理出库详情界面  */
	
	@GetMapping("wlst")
//	@RequiresPermissions("output:outStorageDetail:outStorageDetail")
	String OutStorageDetailWlst(@RequestParam(required = false) Long outstorageId,Model model,HttpSession session){
		session.setAttribute("outId", outstorageId);
	    return "output/outStorageDetail/wlst/outStorageDetail";
	}
	
	/* my 订单管理出库详情界面  */
	
	@GetMapping("my")
//	@RequiresPermissions("output:outStorageDetail:outStorageDetail")
	String OutStorageDetailMy(@RequestParam(required = false) Long outstorageId,Model model,HttpSession session){
		session.setAttribute("outId", outstorageId);
	    return "output/outStorageDetail/my/outStorageDetail";
	}
	
}
